//发货单管理详情（零担）
//杨帆


interface IOrderDeliverManagentBreakBulkViewModel extends ng.IScope{// extends ng.IScope
    /**
     * 订单管理详情
     */
    carrierOrderData: invoiceManagement;
    /**
     * 订单列表
     */
    orderDataList: Array<invoiceManagementOrder>;
    /**
     * 订单数据
     */
    orderData: invoiceManagementOrder;
    /**
     * 总线路列表
     */
    orderTotalList: Array<invoiceManagementOrder>;
    /**
     * 总线路查看详情事件
     */
    orderTotalData: (item: invoiceManagementOrder) => void;
    /**
     * 子线路数据
     */
    orderChildData: Array<invoiceManagementOrderChild>;
    /**
     * 子线路列表
     */
    orderChildList: Array<invoiceManagementOrderChild>;
    /**
    * 子线路查看详情事件
    */
    orderChildDetail: (item: invoiceManagementOrderChild) => void;
    /**
    * 子线路查看详情数据
    */
    orderChildDataDetail: invoiceManagementOrderChild;
    /**
     * 查看详情事件
     */
    orderDetail: (item: invoiceManagementOrder) => void;
    /**
     * app竞价
     */
    orderAppBiddingList: Array<quotationResponse>;
    /**
     * 应收
     */
    orderReceivalbleList: Array<receivableListResponse>;
    /**
     * 合同信息
     */
    contractData: invoiceManagementContract;
    /**
    * 查看详情事件
    */
    showContractDetail: () => void;
    /**
    * 是否显示电子合同
    */
    isContractO: boolean;
    /**
     * 是否显示补充协议
     */
    isAgreementO: boolean;
    /**
    * 电子合同按钮是否启用
    */
    contractT: boolean;
    /**
     * 点击显示电子合同
     */
    ContractO: () => void;
    /**
     * 点击显示补充协议
     */
    AgreementO: () => void;
    /**
    * 是否显示切换补充协议TAB
    */
    isAgreementF: boolean;
    /**
    * 电子合同第一张图
    */
    contractImgA: string;
    /**
    * 电子合同第二张图
    */
    contractImgB: string;
    /**
     * 电子合同第三张图
     */
    contractImgC: string;
    /**
     * 电子合同类型
     */
    eContractType: string;

    /**
    * 电子合同编号
    */
    electronicContractNumber: string;
    /**
     * 电子合同状态
     */
    electronicContractStatus: string;
    /**
     * 补充协议合同编号
     */
    agreementContractNumber: string;
    /**
     * 补充协议合同状态
     */
    agreementContractStatus: string;
    /**
     * 补充协议第一张图
     */
    agreementImgA: string;
    /**
     * 补充协议第二张图
     */
    agreementImgB: string;
    /**
     * 补充协议第三张图
     */
    agreementImgC: string;

    /**
     * 费用信息
     */
    orderCostList: Array<invoiceManagementFee>;
    /**
     * 费用信息查看详情事件
     */
    feeDetail: (feeItem: invoiceManagementFee) => void;
    /**
     * 费用(应付)详情信息
     */
    feeData: invoiceManagementFee;
    /**
     * 线路货物应收列表数据
     */
    receivableData: invoiceManagementReceivable;
    /**
     * 线路货物应收查看详情事件
     */
    receivableDataList: (item: invoiceManagementReceivable) => void;
    /**
     * 退回派车
     */
    backCar: () => void;
    /**
     * 返回
     */
    back: () => void;
    /**
     * 显示/隐藏退回派车
     */
    backShow: boolean;
    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 挂车证上传组件
     */
    trailerUploader: any;
    /**
     * 行驶证上传组件
     */
    travelUploader: any;
    /**
     * 身份证上传组件
     */
    idCardUploader: any;
    /**
     * 车辆照片
     */
    carPicUploader: any;
    /**
     * 道路运输证
     */
    roadTransportCardUploader:any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader:any;
    /**
     * 磅单照片
     */
    weightNoteUploader:any;
    /**
     * 订单附件
     */
    orderUploader: any;
    /**
     * 回单附件
     */
    loaderInfoList: any;
    /**
     * 承运单id
     */
    id: string;
    /**
     * 来货拉用户组
     */
    ccpCarDetali: string;








    //11.1新增
    /**
     * App竞价查看车辆信息
     */
    carOrderDeliverBulkApp: (item: quotationResponse) => void;
    /**
     * 点击查看车辆详情的id
     */
    checkDetailCarId:string;
    /**
     * 车辆id
     */
    carId_:string;
    
    // 11.30新增
     /**
     * 点击附件图片触发
     */
    handleImgClick:(any)=>void;
    /**
     * 点击中用以旋转放大的图片src
     */
    selectedImgSrc:string;
    /**
     * 选中的子单的id
     */
    selectedOrderId:string
     /**
     * 订单是否绑定定位器
     */
    orderBinded:boolean;
    /**
     * 通过定位器定位
     */
    locateByTool: () => void;
    /**
     * 是否显示定位器定位的modal
     */
    showLcoToolMdal: boolean;
    /**
     * 定位器定位的返回按钮
     */
    lt_back: () => void;

    attachList:OssUploaderItem[];
    
    // 回单附件列表
    receiptAttachList:OssUploaderItem[];

   
}






class IOrderDeliverManagentController_breakBulk {

    /**
     * 订单状态
     */
    type: string;
    /**
     * 附件地址
     */
    baseImageUrl: string;
    //隐藏退回派车按钮
    backShow = false;
    //车辆编号
    carId: string;
    //附件
    uploaderList: AddCarAttachmentDetail[] = [];


   


    constructor(private $scope: IOrderDeliverManagentBreakBulkViewModel, private orderService: IorderService, private $location: ng.ILocationService,
        private $ngBootbox: BootboxService, private $state: angular.ui.IStateService, private routeService: routeService, 
        private feeService: IFeeService, private $http: ng.IHttpService, private commonService: ICommonService, private carService: ICarService,
        private areaService: IAreaService,private $timeout: ng.ITimeoutService,private locationService:ILocationService,private ossUploaderService:ossUploaderService) {
        

        this.init();
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";

       

    }

    init(): void {
        
        //关闭弹窗
        window.onhashchange = () => {
            $('#detail').modal('hide');
            $('#orderTotal').modal('hide');
            $('#orderChildren').modal('hide');
        };

        this.$scope.id = this.$location.search().id;
        this.type = this.$location.search().type;

        this.$scope.showContractDetail = this.showContractDetail;
        this.$scope.AgreementO = this.AgreementO;
        this.$scope.ContractO = this.ContractO;
        this.$scope.isContractO = true;
        this.$scope.isAgreementO = false;
        this.$scope.contractT = true;
        this.$scope.isAgreementF = false;
        this.loadDataOrderDeliverBulk();
        
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachList=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader'
            },{
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader'
            },{
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader'
            },{
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUploader'
            },{
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader'
            },{
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader'
            },{
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader'
            },{
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader'
            },{
                title:'磅单照片',
                uploader:null,
                uploaderName:'weightNoteUploader'
            },
        ]
        this.$scope.receiptAttachList=[];
        this.$scope.attachList.forEach((attach,index)=>{
            attach.uploader=new OssFile(this.$scope,attach.uploader,{uploaderList:[],tag:index+1});
        })

        //订单附件
        this.$scope.orderUploader = new OssFile(this.$scope,this.$scope.orderUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});

     
    




        //APP竞价中车辆详情
       
      
       

       

        this.$scope.carOrderDeliverBulkApp=this.carOrderDeliverBulkApp
        // 11.30新增
        this.$scope.handleImgClick=(item)=>{
            this.$scope.selectedImgSrc=item.imgSrc;
        }
        this.$scope.lt_back = () => { $('#locToolLocation').modal('hide'); }
        this.$scope.locateByTool = () => {this.$scope.showLcoToolMdal = true;}
        $('#locToolLocation').on('hidden.bs.modal', (e) => {
            this.$scope.showLcoToolMdal = false
        });

        

    }

    /**
     * 加载数据
     */
    loadDataOrderDeliverBulk = () => {
        this.orderService.invoiceManagementDetail(this.$scope.id).then((res) => {
            //承运单data
            this.$scope.carrierOrderData = res.data;
            this.carService.getDetail(res.data.carId).then(res => {
                this.$scope.ccpCarDetali = res.data.carCode + res.data.driver + res.data.driverPhone;
            })
            //获取订单
            this.$scope.orderDataList = res.data.orderList;
            //订单列表——查看详情
            this.$scope.orderDetail = this.showOrderDetailDialog;
            //合同信息
            this.$scope.contractData = res.data.contract;
            if (this.$scope.contractData != null) {
                this.$scope.electronicContractNumber = res.data.contract.electronicContractNumber;
                if (this.$scope.electronicContractNumber != "") {
                    this.$scope.contractT = false;
                }
                if (this.$scope.electronicContractNumber == "") {//没有电子合同灰显按钮
                   this.$scope.contractT = true;
                }
                this.$scope.agreementContractNumber = res.data.contract.agreementContractNumber;
                if (this.$scope.agreementContractNumber != "") {//没有补充协议
                    this.$scope.isAgreementF = true;
                }
            }

            //付款信息——费用信息
            this.$scope.orderCostList = res.data.feeList;
            //费用信息——查看详情
            this.$scope.feeDetail = this.showFeeDetailDialog;
            /**
             * 承运单附件
             */
            let carArr = res.data.carrierOrderAttachments;
            carArr.forEach((item, index) => {
                this.uploaderList.push(item);
                this.$scope.attachList[Number(item.attachmentTag)-1].uploader.addByAttach(item)
              
            });

            /**
             * 判断是否有“退回派车”按钮
             */
            if(this.$location.search().isReverse){
                window.localStorage.getItem('loginData').toString().indexOf("客服经理") != -1 && res.data.orderList[0].status == "货已送达" && res.data.feeList.length < 1 ? this.$scope.backShow = true : this.$scope.backShow = false;
            }else{
                window.localStorage.getItem("loginData").toString().indexOf("客服经理") != -1 && this.type == "货已送达" && res.data.feeList.length < 1 ? this.$scope.backShow = true : this.$scope.backShow = false;
            }
            

            //退回派车
            this.$scope.backCar = this.backCar;
            //返回
            this.$scope.back = this.back;
            //加载回单附件
            this.getReceiptAttchment(res.data.orderList);
        });
    }

    /**
     * 回单附件
     */
    getReceiptAttchment = (data: invoiceManagementOrder[]) => {
        this.$scope.loaderInfoList = [];
        const OssFile=this.ossUploaderService.getOssFile();

        data.forEach((item, index) => {
            var loadC = [];
            let orderReceiptAttachList:OssUploaderItem[] = [];
            item.childList.forEach((itemC, indeC) => {

                orderReceiptAttachList.push(
                    {
                        title:'回单'+(indeC+1),
                        uploader:null,
                        uploaderName: 'receipt' + indeC+'Uploader'
                    }
                )
                orderReceiptAttachList[indeC].uploader=new  OssFile(this.$scope,orderReceiptAttachList[indeC].uploader,{uploaderList:[],tag:index+1,multiple:true});

            

                itemC.receiptAttachments.forEach((itemR, indexR) => {
                    this.uploaderList.push(itemR);
                    orderReceiptAttachList[indeC].uploader.addByAttach(itemR);
                });
            });
            this.$scope.loaderInfoList.push(orderReceiptAttachList);
        });
    };


    /**
     * 退回派车
     */
    backCar = () => {
        this.$ngBootbox.confirm('确定退回派车吗？').then((res) => {
            this.orderService.managerReturnDispatch(this.$scope.id).then((res) => {
                this.$state.go('app.order.orderDeliverManage', { name: 'Manage' });
            });
        });
    }

    /**
     * 返回
     */
    back = () => {
        if(this.$location.search().name=="reverseComment"){
            this.$state.go('app.eva.evaluate_reverseManage',{ name:'reverseComment' });
            return;
        }
        this.$state.go("app.order.orderDeliverManage", { name: 'Manage' });
    }

    /**
   * 点击显示电子合同
   */
    ContractO = () => {
        if (this.$scope.electronicContractNumber != null && this.$scope.electronicContractNumber != "") {
            this.$scope.isContractO = false;
            this.orderService.EContractDetail(this.$scope.electronicContractNumber).then((res) => {
                this.$scope.contractImgA = res.data.imgList[0];
                this.$scope.contractImgB = res.data.imgList[1];
                this.$scope.contractImgC = res.data.imgList[2];
            }, (rej) => { });

        }
    }
    /**
     * 点击显示补充协议
     */
    AgreementO = () => {
        this.$scope.isAgreementO = true;
        this.$scope.isContractO = false;
        if (this.$scope.agreementContractNumber != "" && this.$scope.agreementContractNumber != null) {
            this.orderService.EContractDetail(this.$scope.agreementContractNumber).then((res) => {
                this.$scope.agreementImgA = res.data.imgList[0];
                this.$scope.agreementImgB = res.data.imgList[1];
                this.$scope.agreementImgC = res.data.imgList[2];
                // let agreementContractNumber = this.$scope.agreementContractNumber;
            }, (rej) => { });
        }

    }

    /**
   * 查看电子合同详情
   */
    showContractDetail = () => {
        if (this.$scope.electronicContractNumber != null && this.$scope.electronicContractNumber != "") {
            this.orderService.EContractDetail(this.$scope.electronicContractNumber).then((res) => {
                this.$scope.contractImgA = res.data.imgList[0];
                this.$scope.contractImgB = res.data.imgList[1];
                this.$scope.contractImgC = res.data.imgList[2];

                $('#Econtract').modal('show');
            }, (rej) => { });
        }
    }

    /**
     * 查看详情
     */
    showOrderDetailDialog = (item: invoiceManagementOrder) => {

        this.$scope.selectedOrderId=item.id;
        /**
         * 获取订单数据（总单数据）
         */
        this.$scope.orderData = item;
        /**
         * 查看总线路详情详情事件
         */
        this.$scope.orderTotalData = this.showOrderTotalData;
        /**
         * 总线路列表
         */
        this.$scope.orderTotalList = [item];
        /**
         * 子线路数据
         */
        this.$scope.orderChildData = item.childList;
        /**
         * 子线路列表
         */
        this.$scope.orderChildList = item.childList;
        /**
         * 子线路查看详情事件
         */
        this.$scope.orderChildDetail = this.showOrderChildDetail;
        /**
         * app竞价
         */
        this.orderService.quotation(item.id).then((res) => {
            this.$scope.orderAppBiddingList = res.data.data;
        });

        /**
         * 应收信息
         */
        let receivalbleArr = [];
        [item].forEach((receivable, index) => {
            /**
             * 订单应收
             */
            if (receivable.receivableList.length > 0) {
                receivable.receivableList.forEach((itemR, index) => {
                    receivalbleArr.push(itemR);
                });
            }
            /**
             * 子单应收
             */
            receivable.childList.forEach((chilItem, index) => {
                if (chilItem.receivableList.length > 0) {
                    chilItem.receivableList.forEach((itemRC, index) => {
                        receivalbleArr.push(itemRC);
                    });
                }
            });

        });
        this.$scope.orderReceivalbleList = receivalbleArr;

        this.$scope.orderUploader.clearQueue();
        //订单附件
    

        /**
         * 订单附件
         */
        let orderArr = item.orderAttachments;
        orderArr.forEach((itemO, index) => {
            this.uploaderList.push(itemO);
            // this.commonService.showFileItem(itemO, this.$scope.orderUploader);
            this.$scope.orderUploader.addByAttach(itemO)
        });

        /**
         *  线路货物应收查看详情事件
         */
        this.$scope.receivableDataList = this.showReceivableDataList;
        
        this.$scope.orderBinded=false;
        this.locationService.checkOrderBinded(item.id,!(item.status==="货已送达")).then(res=>{
            // console.log(res.data)
            if(res.data.extData){
                this.$scope.orderBinded=true;
            }
        })

    }

    /**
     * 费用（付款）详情
     */
    showFeeDetailDialog = (feeItem: invoiceManagementFee) => {
        /**
         * 获取费用数据
         */
        this.$scope.feeData = feeItem;
    }

    /**
     *  线路货物应收查看详情事件
     */
    showReceivableDataList = (item: invoiceManagementReceivable) => {
        this.$scope.receivableData = item;
    }

    /**
     * 子线路查看详情事件
     */
    showOrderChildDetail = (item: invoiceManagementOrderChild) => {
        /**
         * 获取子线路详情数据
         */
        this.$scope.orderChildDataDetail = item;
    }

    /**
     * 总线路查看详情事件
     */
    showOrderTotalData = (item: invoiceManagementOrder) => {
        this.$scope.orderData = item;
    }






    //11.1新增

    /**
     * 获取 App竞价 车辆详情
     */
    carOrderDeliverBulkApp=(item: quotationResponse)=>{
       
        this.$scope.checkDetailCarId = item.driverUserId;

  
    }


}

angular.module("tms").controller("orderDeliverManagentController_breakBulk", ["$scope", "orderService", "$location", "$ngBootbox", "$state", "routeService", "feeService", "$http", "commonService", "carService",
"areaService","$timeout","locationService","ossUploaderService", IOrderDeliverManagentController_breakBulk]);